#coding=utf-8 


"""
安装 pexpect 模块  
Unix 或者 linux

"""

"""
D:\Python26\python.exe  D:\Python26\Lib\site-packages\easy_install.py  pexpect 

D:\Python26\python.exe  D:\Python26\Lib\site-packages\easy_install.py  winpexpect 


 
先安装   setuptools-0.6c10.win32-py2.6.exe
先安装   pywin32




安装 winpexpect 模块  
windows下支持

http://pypi.python.org/packages/source/w/winpexpect/ 下载



 

cd  /d    D:\TEMP\winpexpect-1.3
D:\Python26\python.exe   setup.py  install


？   ImportError: No module named setuptools
！   安装 setuptools 拷贝 Lib到python的安装目录


    
D:\Python26\python.exe  E:\py_dev\ssh_pexpect.py

"""



import sys

import winpexpect


#import pexpect  

sys.exit();
def ssh_cmd(ip, user, passwd, cmd):
    ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
    r = ''
    try:
        i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
        if i == 0 :
            ssh.sendline(passwd)
        elif i == 1:
            ssh.sendline('yes')
    except pexpect.EOF:
        ssh.close()
    else:
        r = ssh.read()
        ssh.expect(pexpect.EOF)
        ssh.close()
    return r

hosts = """
192.168.0.120:root:oracle:df -h,uptime
"""

for host in hosts.split("\n"):
    if host:
        ip, user, passwd, cmds = host.split(":")
        for cmd in cmds.split(","):
            print "-- %s run:%s --" % (ip, cmd)
            print ssh_cmd(ip, user, passwd, cmd)                                


